<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min(1).css">
    <link rel="stylesheet" href="../css/style.css">

    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
    </style>


</head>


<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理<small>角色管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">


                <el-input placeholder="角色名称/角色关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage = 1; findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>


                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
                <el-table-column prop="keyword" label="角色关键字" align="center"></el-table-column>
                <el-table-column prop="description" label="角色描述" align="center"></el-table-column>


                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>


            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->

            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">

                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取消</el-button>
                                    <el-button type="primary" @click="handleAdd()">确定</el-button>
                                </div>

                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <tbody>
                                        <tr>
                                            <td>选择</td>
                                            <td> 权限名称</td>
                                            <td>关键字</td>
                                            <td>权限说明</td>
                                        </tr>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label>{{c.name}}</label></td>
                                            <td><label>{{c.keyword}}</label></td>
                                            <td style="left: auto"><label>{{c.description}}</label></td>
                                        </tr>
                                        </tbody>

                                    </table>
                                </div>

                            </el-tab-pane>


                            <el-tab-pane label="菜单信息" name="third">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <tbody>
                                        <tr>
                                            <td>选择</td>
                                            <td>菜单名称</td>
                                            <td>优先级</td>
                                            <td>自关联Id</td>
                                            <td>描述</td>
                                        </tr>
                                        <tbody>
                                        <tr v-for="c in menuDate">
                                            <td>
                                                <input :id="c.id" v-model="menuIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label>{{c.name}}</label></td>
                                            <td><label>{{c.priority}}</label></td>
                                            <td><label>{{c.parentMenuId}}</label></td>
                                            <td style="left: auto"><label>{{c.description}}</label></td>
                                        </tr>


                                        </tbody>

                                    </table>

                                </div>

                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <template>


                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">

                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取消</el-button>
                                    <el-button type="primary" @click="handleAdd()">确定</el-button>
                                </div>

                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <tbody>
                                        <tr>
                                            <td>选择</td>
                                            <td> 权限名称</td>
                                            <td>关键字</td>
                                            <td>权限说明</td>
                                        </tr>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label>{{c.name}}</label></td>
                                            <td><label>{{c.keyword}}</label></td>
                                            <td style="left: auto"><label>{{c.description}}</label></td>
                                        </tr>
                                        </tbody>

                                    </table>
                                </div>

                            </el-tab-pane>


                            <el-tab-pane label="菜单信息" name="third">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <tbody>
                                        <tr>
                                            <td>选择</td>
                                            <td>菜单名称</td>
                                            <td>优先级</td>
                                            <td>自关联Id</td>
                                            <td>描述</td>
                                        </tr>
                                        <tbody>
                                        <tr v-for="d in menuDate">
                                            <td>
                                                <input :id="d.id" v-model="menuIds" type="checkbox" :value="d.id">
                                            </td>
                                            <td><label>{{d.name}}</label></td>
                                            <td><label>{{d.priority}}</label></td>
                                            <td><label>{{d.parentMenuId}}</label></td>
                                            <td style="left: auto"><label>{{d.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>

                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>

                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>         var vue = new Vue({
    el: '#app',
    data: {
        pagination: {currentPage: 1, pageSize: 8, total: 0, queryString: null},
        dataList: [],
        formData: {},
        tableData: [],
        menuDate: [],
        permissionIds: [],
        menuIds: [],
        dialogFormVisible: false,
        dialogFormVisible4Edit: false,
        rules: {
            name: [{required: true, message: '角色名称为必填项', trigger: 'blur'}],
            keyword: [{required: true, message: '角色关键字为必填项', trigger: 'blur'}]
        }
    },
    created() {
        this.findPage();
    },
    methods: {
        showMessage(r) {
            if (r == 'Error: Request failed with status code 403') {
                this.$message.error('无访问权限');
                return;
            } else {
                this.$message.error('未知错误');
                return;
            }
        }, handleEdit() {
            axios.post("/role/edit?permissionIds=" + this.permissionIds + "&menuIds=" + this.menuIds, this.formData).then((resp) => {
                if (resp.data.flag) {
                    this.$message({message: resp.data.message, type: "success"});
                } else {
                    this.$message.error(resp.data.message);
                }
            }).finally(() => {
                this.dialogFormVisible4Edit = false;
                this.findPage();
            });
        }, handleAdd() {
            axios.post("/role/add?permissionIds=" + this.permissionIds + "&menuIds=" + this.menuIds, this.formData).then((resp) => {
                if (resp.data.flag) {
                    this.$message({message: resp.data.message, type: "success"});
                } else {
                    this.$message.error(resp.data.message)
                }
            }).finally(() => {
                this.dialogFormVisible = false;
                this.findPage();
            });
        }, findPage() {
            let param = {
                currentPage: this.pagination.currentPage,
                pageSize: this.pagination.pageSize,
                queryString: this.pagination.queryString
            };
            axios.post("/role/pageQuery", param).then((resp) => {
                this.pagination.total = resp.data.total;
                this.dataList = resp.data.rows;
            });
        }, resetForm() {
            this.formData = {};
            this.permissionIds = [];
            this.menuIds = [];
        }, handleCreate() {
            if (this.$refs.dataAddForm) {
                this.$refs.dataAddForm.resetFields();
            }
            ;this.dialogFormVisible = true;
            this.activeName = "first";
            this.resetForm();
            axios.get("/permission/findAll").then((resp) => {
                if (resp.data.flag) {
                    this.tableData = resp.data.data;
                } else {
                    this.$message.error(resp.data.message);
                }
            });
            axios.get("/menu/findAll").then((resp) => {
                if (resp.data.flag) {
                    this.menuDate = resp.data.data;
                } else {
                    this.$message.error(resp.data.message);
                }
            });
        }, handleUpdate(row) {
            if (this.$refs.dataEditForm) {
                this.$refs.dataEditForm.resetFields();
            }
            ;this.dialogFormVisible4Edit = true;
            this.activeName = 'first';
            axios.get("/role/findById?id=" + row.id).then((resp) => {
                if (resp.data.flag) {
                    this.formData = resp.data.data;
                } else {
                    this.message.error(resp.data.message);
                }
            });
            axios.get("/permission/findAll").then((resp) => {
                if (resp.data.flag) {
                    this.tableData = resp.data.data;
                    axios.get("/role/findPermissionIdsByRoleId?id=" + row.id).then((resp) => {
                        if (resp.data.flag) {
                            this.permissionIds = resp.data.data;
                        } else {
                            this.$message.error(resp.data.message);
                        }
                    });
                } else {
                    this.$message.error(resp.data.message);
                }
            });
            axios.get("/menu/findAll").then((resp) => {
                if (resp.data.flag) {
                    this.menuDate = resp.data.data;
                    axios.post("/role/findMenuIdsByRoleId?id=" + row.id).then((resp) => {
                        this.menuIds = resp.data.data;
                    });
                } else {
                    this.$message.error(resp.data.message);
                }
            });
        }, handleCurrentChange(currentPage) {
            this.pagination.currentPage = currentPage;
            this.findPage();
        }, handleDelete(row) {
            this.$confirm("你确定要删除" + row.name + "的全部信息吗？", "提示", {type: 'warnig'}).then(() => {
                axios.get("/role/delete?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        this.$message({type: 'success', message: res.data.message});
                        this.findPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).catch((r) => {
                    this.showMessage(r);
                });
            }).catch(() => {
                this.$message({type: 'info', message: '操作已取消'});
            });
        }
    }
})     </script>
</html>
